package com.hotel.config.rabbitmq.dead;

import com.common.status.ResultMsg;
import com.hotel.config.rabbitmq.config.PersonalCouponCancelDirectConfig;
import com.hotel.config.rabbitmq.config.PersonalCouponWebSocketDirectConfig;
import com.hotel.config.websocket.WebSocketProcess;
import com.hotel.customersystem.pojo.CustomerUserCoupon;
import com.hotel.customersystem.service.CustomerUserCouponService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class PersonalCouponCancelDead {

    @Resource
    private WebSocketProcess webSocketProcess;

    @Resource
    private CustomerUserCouponService customerUserCouponService;

    /**
     * 优惠劵过期修改状态
     */
    @RabbitHandler
    @RabbitListener(queues = PersonalCouponCancelDirectConfig.DEAD_PERSON_COUPON_CANCEL_QUEUE)
    public void personCouponWebSocketListener(Message message, Channel channel){
        System.out.println("死信队列(优惠劵过期)进行监听..."+new String(message.getBody()));
        CustomerUserCoupon customerUserCoupon = customerUserCouponService.getById(new String(message.getBody()));
//        System.out.println(customerUserCoupon);
        customerUserCoupon.setCouponUseStatus(2);
        customerUserCouponService.updateById(customerUserCoupon);
        //消息确认
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
